<template>
  <div class="login-account">
    <el-form
      label-width="80px"
      class="demo-ruleForm"
      :rules="rules"
      :model="account"
      ref="elFormRef"
    >
      <el-form-item label="账户" prop="name">
        <el-input v-model="account.name"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="account.password" show-password></el-input>
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts">
import localCache from '@/utils/JGLocalCache'
import { useStore } from 'vuex'
import { ElForm } from 'element-plus'
import { defineComponent, reactive, ref } from 'vue'
import { rules } from '../config/account-rules'
export default defineComponent({
  setup() {
    const store = useStore()

    const account = reactive({
      name: localCache.getCache('username') ?? '',
      password: localCache.getCache('password') ?? ''
    })
    const elFormRef = ref<InstanceType<typeof ElForm>>()
    const loginAction = (isKeepPassword: boolean) => {
      // console.log('执行accountlogin动作')
      elFormRef.value?.validate((valid) => {
        // console.log(valid)
        if (valid) {
          //判断是否需要记住密码
          if (isKeepPassword) {
            //本地缓存
            localCache.setCache('username', account.name)
            localCache.setCache('password', account.password)
          } else {
            localCache.deleteCache('username')
            localCache.deleteCache('password')
          }
          // 2.开始进行登录验证
          store.dispatch('loginModule/accountLoginAction', { ...account })
        }
      })
    }
    return { account, rules, loginAction, elFormRef }
  }
})
</script>

<style scoped></style>
